package com.biotools.meerkat;

import java.io.*;
import java.util.*;

public final class Hand implements Serializable
{
    public static final int MAX_CARDS = 7;
    private int[] cards;
  
    
    public Hand() {
        super();
        (this.cards = new int[8])[0] = 0;
    }
    
    public Hand(final String cs) {
        super();
        (this.cards = new int[8])[0] = 0;
        final StringTokenizer t = new StringTokenizer(cs, " -");
        while (t.hasMoreTokens()) {
            final String s = t.nextToken();
            if (s.length() == 2) {
                final Card c = new Card(s.charAt(0), s.charAt(1));
                if (c.getIndex() == -1) {
                    continue;
                }
                this.addCard(c);
            }
        }
    }
    
    public Hand(final Hand h) {
        super();
        (this.cards = new int[8])[0] = h.size();
        for (int i = 1; i <= this.cards[0]; ++i) {
            this.cards[i] = h.cards[i];
        }
    }
    
    public void clear() {
        this.cards[0] = 0;
    }
    
    public final int size() {
        return this.cards[0];
    }
    
    public void removeCard() {
        assert this.cards[0] > 0;
        final int[] cards = this.cards;
        final int n = 0;
        --cards[n];
    }
    
    public void makeEmpty() {
        this.cards[0] = 0;
    }
    
    public boolean addCard(final Card c) {
        if (c == null || this.cards[0] == 7) {
            return false;
        }
        final int[] cards = this.cards;
        final int n = 0;
        ++cards[n];
        this.cards[this.cards[0]] = c.getIndex();
        return true;
    }
    
    public boolean addCard(final int i) {
        if (this.cards[0] == 7) {
            return false;
        }
        final int[] cards = this.cards;
        final int n = 0;
        ++cards[n];
        this.cards[this.cards[0]] = i;
        return true;
    }
    
    public Card getCard(final int pos) {
        assert pos >= 1 && pos <= this.cards[0];
        return new Card(this.cards[pos]);
    }
    
    public int getCardIndex(final int pos) {
        return this.cards[pos];
    }
    
    public int getLastCardIndex() {
        return this.cards[this.cards[0]];
    }
    
    public void setCard(final int pos, final Card c) {
        if (this.cards[0] < pos) {
            this.cards[0] = pos;
        }
        this.cards[pos] = c.getIndex();
    }
    
    public void setCard(final int pos, final int cInd) {
        if (this.cards[0] < pos) {
            this.cards[0] = pos;
        }
        this.cards[pos] = cInd;
    }
    
    public int[] getCardArray() {
        return this.cards;
    }
    
    public void sort() {
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < this.cards[0]; ++i) {
                if (this.cards[i] < this.cards[i + 1]) {
                    flag = true;
                    final int t = this.cards[i];
                    this.cards[i] = this.cards[i + 1];
                    this.cards[i + 1] = t;
                }
            }
        }
    }
    
    public boolean equals(final Hand h) {
        if (h.size() != this.cards[0]) {
            return false;
        }
        for (int i = 1; i <= this.cards[0]; ++i) {
            if (this.cards[i] != h.cards[i]) {
                return false;
            }
        }
        return true;
    }
    
    public String toString() {
        String s = new String();
        for (int i = 1; i <= this.cards[0]; ++i) {
            s = String.valueOf(s) + " " + this.getCard(i).toString();
        }
        return s.trim();
    }
    
    public String flashingString() {
        String s = new String();
        for (int i = 1; i <= this.cards[0]; ++i) {
            final String str = this.getCard(i).toString();
            if (!str.equals("??")) {
                s = String.valueOf(s) + " " + str;
            }
        }
        return s.trim();
    }
    
    public boolean contains(final Card c) {
        if (c == null) {
            return false;
        }
        for (int i = 1; i <= this.size(); ++i) {
            if (this.getCard(i).equals(c)) {
                return true;
            }
        }
        return false;
    }
    
    public void addHand(final Hand h) {
        for (int i = 1; i <= h.size(); ++i) {
            this.addCard(h.getCard(i));
        }
    }
    
    public void clearBadCards() {
        for (int i = this.size(); i > 0; --i) {
            if (!this.getCard(i).valid()) {
                this.removeCard(i);
            }
        }
    }
    
    public void removeCard(final int i) {
        for (int j = i; j < this.size(); ++j) {
            this.cards[j] = this.cards[j + 1];
        }
        if (this.cards[0] > 0) {
            final int[] cards = this.cards;
            final int n = 0;
            --cards[n];
        }
    }
    
    public static String getCardString(Card c1, Card c2) {
        if (!c1.valid() || !c2.valid()) {
            return "??";
        }
        if (c1.getRank() < c2.getRank()) {
            final Card t = c1;
            c1 = c2;
            c2 = t;
        }
        if (c1.getRank() == c2.getRank()) {
            return String.valueOf(Card.getRankChar(c1.getRank())) + Card.getRankChar(c2.getRank());
        }
        if (c1.getSuit() == c2.getSuit()) {
            return String.valueOf(Card.getRankChar(c1.getRank())) + Card.getRankChar(c2.getRank()) + "s";
        }
        return String.valueOf(Card.getRankChar(c1.getRank())) + Card.getRankChar(c2.getRank()) + "o";
    }
    
    public final Card getLastCard() {
        return this.getCard(this.cards[0]);
    }
    
    public Card getFirstCard() {
        return this.getCard(1);
    }
    
    public Card getSecondCard() {
        return this.getCard(2);
    }
}
